<?php

/**
 * This is the model class for table "{{depth}}".
 *
 * The followings are the available columns in table '{{depth}}':
 * @property integer $id
 * @property integer $pid
 * @property integer $cid
 * @property string $name
 * @property integer $sort
 * @property integer $state
 */
class Depth extends CDbActiveRecord {
	/**
	 * 缓存方式载入职位数据
	 *
	 * @return array $data 返回职位数据
	 */
	public function loadData() {
		// Yii::app ()->memcache->delete ( get_class ( $this ) );
		$cachekey = get_class ( $this );
		if (false === ($data = Yii::app ()->memcache->get ( $cachekey ))) {
			$data = self::model ()->published ()->findAll ( array (
					'limit' => 5
			) );
			Yii::app ()->memcache->set ( $cachekey, $data, 3600 * 24 );
		}
		// print_r ( $cacheData );
		return $data;
	}
	/**
	 * 删除职位缓存
	 */
	public function delete() {
		Yii::app ()->memcache->delete ( get_class ( $this ) );
	}

	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	public function tableName() {
		return '{{depth}}';
	}
	
	public function rules() {
		return array (
				array (
						'pid, cid, name',
						'required' 
				),
				array (
						'pid, cid, sort, state',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'name',
						'length',
						'max' => 40 
				),
				array (
						'id, pid, cid, name, sort, state',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	public function relations() {
		return array ();
	}
	public function scopes() {
		return array (
				'published' => array (
						'condition' => 'state=1' 
				) 
		);
	}
	public function attributeLabels() {
		return array (
				'id' => 'ID',
				'pid' => 'Pid',
				'cid' => 'Cid',
				'name' => 'Name',
				'sort' => 'Sort',
				'state' => 'State' 
		);
	}
	public function search() {
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		$criteria->compare ( 'pid', $this->pid );
		$criteria->compare ( 'cid', $this->cid );
		$criteria->compare ( 'name', $this->name, true );
		$criteria->compare ( 'sort', $this->sort );
		$criteria->compare ( 'state', $this->state );
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	}
	//其他代码
	
	public function beforeSave()
	{
		parent::beforeSave();
		//自己的行为事件
	}
	
	public function defaultScope(){
		return $this->getDefaultScopeDisabled() ? array() : array('condition'=>'state=1');
	}
	
	public function resetScope(){
		if(null!==($criteria=$this->getDbCriteria(false))){
			foreach(get_class_vars('CDbCriteria') as $k=>$v)
				$criteria->$k=$v;
		}
		return $this;
	}
}